package com.engine.salary.entity.salarysob.bo;

import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.salarysob.param.SalarySobRangeSaveParam;
import com.engine.salary.entity.salarysob.po.SalarySobRangePO;
import com.engine.salary.enums.salarysob.SalaryEmployeeStatusEnum;
import com.engine.salary.enums.salarysob.TargetTypeEnum;
import com.engine.salary.util.SalaryEntityUtil;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 薪资账套的人员范围保存
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalarySobRangeSaveBO {

    /**
     * 处理前端传回的保存参数，转换成对应的po（如果保存参数中的人员、部门、岗位等之前就已经添加过了，就不需要再次保存了）
     *
     * @param salarySobRanges
     * @param saveParam
     * @param employeeId
     * @return
     */
    public static Result handle(List<SalarySobRangePO> salarySobRanges, SalarySobRangeSaveParam saveParam, Long employeeId, boolean isImport) {
        Date now = new Date();
        Result handleResult = Result.builder()
                .needInsertSalarySobRanges(Lists.newArrayList())
                .needUpdateSalarySobRanges(Lists.newArrayList())
                .build();
        if (CollectionUtils.isEmpty(saveParam.getTargetParams())) {
            return handleResult;
        }
        Map<String, SalarySobRangePO> salarySobRangeMap = SalaryEntityUtil.convert2Map(salarySobRanges, e -> e.getIncludeType() + "-" + e.getTargetType() + "-" + e.getTargetId());
        for (SalarySobRangeSaveParam.SalarySobRangeTargetParam targetParam : saveParam.getTargetParams()) {
            if (targetParam.getTargetType() == TargetTypeEnum.SQL) {
                SalarySobRangePO salarySobRangePO = SalarySobRangePO.builder()
                        .salarySobId(saveParam.getSalarySobId())
                        .targetType(targetParam.getTargetType().getValue())
                        .targetId(0L)
                        .target(targetParam.getTarget())
//                    .employeeStatuses(parseEnumListToStr(saveParam.getEmployeeStatus()))
//                    .employeeStatus(saveParam.getEmployeeStatus().getValue())
                        .includeType(saveParam.getIncludeType())
                        .creator(employeeId)
                        .createTime(now)
                        .updateTime(now)
                        .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                        .deleteType(0)
                        .build();

                if (isImport) {
                    salarySobRangePO.setEmployeeStatuses(parseEnumListToStr(targetParam.getEmployeeStatus()));
                } else {
                    salarySobRangePO.setEmployeeStatuses(parseEnumListToStr(saveParam.getEmployeeStatus()));
                }

                handleResult.getNeedInsertSalarySobRanges().add(salarySobRangePO);
            }else {
                String key = saveParam.getIncludeType() + "-" + targetParam.getTargetType().getValue() + "-" + targetParam.getTargetId();
                if (salarySobRangeMap.containsKey(key)) {
                    SalarySobRangePO salarySobRangePO = salarySobRangeMap.get(key);
                    if (isImport) {
                        salarySobRangePO.setEmployeeStatuses(parseEnumListToStr(targetParam.getEmployeeStatus()));
                    } else {
                        salarySobRangePO.setEmployeeStatuses(parseEnumListToStr(saveParam.getEmployeeStatus()));
                    }
                    salarySobRangePO.setUpdateTime(now);
                    handleResult.getNeedUpdateSalarySobRanges().add(salarySobRangePO);
                    continue;
                }
                SalarySobRangePO salarySobRangePO = SalarySobRangePO.builder()
                        .salarySobId(saveParam.getSalarySobId())
                        .targetType(targetParam.getTargetType().getValue())
                        .targetId(targetParam.getTargetId())
                        .target(targetParam.getTarget())
//                    .employeeStatuses(parseEnumListToStr(saveParam.getEmployeeStatus()))
//                    .employeeStatus(saveParam.getEmployeeStatus().getValue())
                        .includeType(saveParam.getIncludeType())
                        .creator(employeeId)
                        .createTime(now)
                        .updateTime(now)
                        .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                        .deleteType(0)
                        .build();

                if (isImport) {
                    salarySobRangePO.setEmployeeStatuses(parseEnumListToStr(targetParam.getEmployeeStatus()));
                } else {
                    salarySobRangePO.setEmployeeStatuses(parseEnumListToStr(saveParam.getEmployeeStatus()));
                }

                handleResult.getNeedInsertSalarySobRanges().add(salarySobRangePO);
            }
        }
        return handleResult;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Result {

        /**
         * 需要更新的人员范围
         */
        private Collection<SalarySobRangePO> needUpdateSalarySobRanges;

        /**
         * 需要新增的人员范围
         */
        private Collection<SalarySobRangePO> needInsertSalarySobRanges;
    }

    public static String parseEnumListToStr(SalaryEmployeeStatusEnum[] salaryEmployeeStatusEnums) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < salaryEmployeeStatusEnums.length; i++) {
            sb.append(salaryEmployeeStatusEnums[i].getValue().toString());
            if (i + 1 != salaryEmployeeStatusEnums.length) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

}
